package pl.edu.mimuw.irs.rcp.controller.editor;

import java.util.List;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorSite;
import org.eclipse.ui.ISelectionListener;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.part.EditorPart;

import pl.edu.mimuw.irs.core.model.analysis.History;
import pl.edu.mimuw.irs.core.model.analysis.Report;
import pl.edu.mimuw.irs.core.model.design.processing.Resource;
import pl.edu.mimuw.irs.rcp.controller.editor.input.ReportEditorInput;
import pl.edu.mimuw.irs.rcp.util.Pair;

/**
 * @author Jakub Rauch
 * Created on: 2009-05-25
 */
public class ReportEditor extends EditorPart {

	public static final String ID = "pl.edu.mimuw.irs.editor.report";
	public static final String COMMAND_ID = "pl.edu.mimuw.irs.command.editreport";

	// *** Attributes
	
	private TableViewer viewer;
	private Report model;
	private ISelectionListener listener = new ISelectionListener() {
		@Override
		public void selectionChanged(IWorkbenchPart sourcepart, ISelection selection) {
			// we ignore our own selections
			if (sourcepart != ReportEditor.this) {
			    //showSelection(sourcepart, selection);
			}
		}
	};
	
	// *** Constructors

	public ReportEditor() {
		super();
	}

	// *** Methods

	@Override
	public void doSave(IProgressMonitor monitor) {
		// XXX : container editor save
	}

	@Override
	public void doSaveAs() {
		// XXX : Auto-generated method stub
	}

	@Override
	public void init(IEditorSite site, IEditorInput input)
			throws PartInitException {
		setSite(site);
		setInput(input);
		this.model = (((ReportEditorInput) input).getReport());
	}

	@Override
	public boolean isDirty() {
		// XXX todo
		return true;
	}

	@Override
	public boolean isSaveAsAllowed() {
		// XXX save allowed
		return true;
	}

	/**
	 * This is a callback that will allow us to create the viewer and initialize
	 * it.
	 */
	public void createPartControl(Composite parent) {
		viewer = new TableViewer(parent, SWT.MULTI | SWT.H_SCROLL
				| SWT.V_SCROLL | SWT.BORDER | SWT.FULL_SELECTION);
		//viewer.setContentProvider(new ResourcesViewContentProvider(viewer));
		//viewer.setLabelProvider(new ResourcesViewLabelProvider());
		//viewer.setInput(null);
		getSite().setSelectionProvider(viewer);
		getSite().getWorkbenchWindow().getSelectionService().addSelectionListener(listener);
		
		
		Table table = viewer.getTable();
		table.setLinesVisible (true);
		table.setHeaderVisible (true);
		GridData data = new GridData(SWT.FILL, SWT.FILL, true, true);
		data.heightHint = 200;
		table.setLayoutData(data);
		String[] titles = {"Total time", "Usage cost", 
				"1st expense", "2nd expense", "3rd expense", "Should be empty", "Steps"};
		for (int i=0; i < titles.length; i++) {
			TableColumn column = new TableColumn (table, SWT.NONE);
			column.setText (titles [i]);
		}
		for (History history : model.getCheapest()) {
			List<Pair<Resource, Integer>> mostExpensive = history.getMostExpensive();
			TableItem item = new TableItem (table, SWT.NONE);
			item.setText(0, "" + history.getTimestamp().getValue());
			item.setText(1, "" + history.getTotalUsageCost());
			if (mostExpensive.size() > 0)
				item.setText(2, "" + mostExpensive.get(0));
			if (mostExpensive.size() > 1)
				item.setText(3, "" + mostExpensive.get(1));
			if (mostExpensive.size() > 2) {
				item.setText(4, "" + mostExpensive.get(2));
				item.setText(5, "" + history.getBusyActivities());
				item.setText(6, "" + history.getChronology());
			}
		}
		for (int i=0; i<titles.length; i++) {
			table.getColumn(i).pack ();
		}
		
		long diff = (model.getEnd().getTime() - model.getStart().getTime());
		setContentDescription("Generation took : " + 
				(diff / 1000) + "," + 
				(diff % 1000) + " secs");
	}

	

	/**
	 * Passing the focus request to the viewer's control.
	 */
	public void setFocus() {
		viewer.getControl().setFocus();
	}
	
	/*
	 * Progress monitor
	 */
	@Override
	public void showBusy(boolean busy) {
		super.showBusy(busy);
	    if (busy)
	        setPartName("Generating report"); // XXX: messages.properties
	    else
	        setPartName("Report summary"); 
	}
	
}
